import unittest
import requests
import app
from api.loginApi import LoginApi
from data.login_db import login_data
from parameterized import parameterized


def get_data():
    test_data = []
    data = login_data()
    for i in data:
        test_data.append(i)
    return test_data


class TestLogin(unittest.TestCase):
    def setUp(self):
        self.api = LoginApi()
        self.session = requests.Session()

    def tearDown(self):
        if self.session:
            self.session.close()

    @parameterized.expand(get_data())
    def test02_login(self, num, name, username, password, vertify, status_code, status, msg, url):
        respond_v = self.api.get_vertify(self.session)
        print(respond_v.headers)
        self.assertIn("image/png", respond_v.headers.get('Content-Type'))

        respond_l = self.api.get_login(self.session, username, password, vertify)
        print(str(num) + "名称：" + name)
        print(respond_l.json())
        self.assertEqual(status_code, respond_l.status_code)
        self.assertEqual(status, respond_l.json().get('status'))
        if respond_l.json().get('status') != 1:
            self.assertIn(msg, respond_l.json().get('msg'))
        else:
            self.assertIn(url, respond_l.json().get('url'))
            app.login_success = respond_l.json().get('url')
            print(respond_l.headers)
            respond_m = self.api.get_login_success(self.session, app.login_success)
            print(respond_m.headers)
        print(self.session.cookies)


'''    @parameterized.expand(get_data())
    def test02_login(self, num, name, username, password, vertify, status_code, status, msg, url):
        respond = self.api.get_login(self.session, username, password, vertify)
        print(str(num) + "名称：" + name)
        print(respond.json())
        self.assertEqual(status_code, respond.status_code)
        self.assertEqual(status, respond.json().get('status'))
        if respond.json().get('status') != 1:
            self.assertIn(msg, respond.json().get('msg'))
        else: self.assertIn(url, respond.json().get('url'))
'''
